package com.sykj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.bean.card.CardGame;
import com.sykj.service.CardGameService;
import com.sykj.util.RedisByhUtils;
import com.sykj.util.RedisKeys;
import com.sykj.util.reponse.CommonEnum;
import com.sykj.util.reponse.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: BeautyClub
 * @description:
 * @author: qiaoli
 * @create: 2020-08-07 09:46
 */

@RestController
@RequestMapping("/game")
public class GameController {
@Autowired
    CardGameService  cardGameService;
@Autowired
    RedisByhUtils  redisByhUtils;
    /**分页查询
     * 功能描述:  status :活动状态  curpage: 第几页
     * limit  每页几条
     * @Param: [status, curpage, limit]
     * @Return: com.sykj.util.reponse.ResultBody
     * @Author: Feiwo Sicun
     * @Date: 2020/8/7 9:55
     */
    @GetMapping("/list/{status}/{curpage}/{limit}")
    public ResultBody   queryList(@PathVariable int status, @PathVariable int curpage, @PathVariable int limit){

        Date now= new Date();

        QueryWrapper queryWrapper=new QueryWrapper();
        switch (status) {
            case -1:
                //查全部
                break;
            case 0:
                //未开始
                queryWrapper.gt("starttime ",now);
                break;
            case 1:
                //进行中
                queryWrapper.le("starttime ",now);
                queryWrapper.gt("endtime",now);
                break;
            case 2:
                //已结束
               queryWrapper.le("endtime",now);
                break;
        }
        queryWrapper.orderByDesc("starttime");

        if (curpage > 0 && limit > 0){
            PageHelper.startPage(curpage,limit);
        }

        List list = cardGameService.list(queryWrapper);
        PageInfo<CardGame> pageInfo=new PageInfo(list);
        return new ResultBody(CommonEnum.SUCCESS,pageInfo);

    }
/**
 * 功能描述: 判断活动是否存在
 * @Param: [id]
 * @Return: com.sykj.util.reponse.ResultBody
 * @Author: Feiwo Sicun
 * @Date: 2020/8/8 14:45
 */
     @GetMapping("/querygameBy/{id}")
  public  ResultBody  QueryGame(@PathVariable("id")String id){
     Date date= new Date();
       // 先判断redis中是否将此活动 预热了
         if(!redisByhUtils.hasKey(RedisKeys.INFO+id)){
             //若不存在，查询出此活动信息；
             CardGame byId = cardGameService.getById(id);
             return new ResultBody(CommonEnum.GAME_ACT_HDWKS,byId);
         }
         //若存在，将信息取出来
        CardGame cardGame= (CardGame) redisByhUtils.get(RedisKeys.INFO+id);

    // 判断活动是否过期
         //活动未开始
         if(cardGame.getStarttime().after(date)){
             CardGame byId = cardGameService.getById(id);
             return new ResultBody(CommonEnum.GAME_ACT_HDWKS,byId);
         }
         //活动已结束
         if(date.after(cardGame.getEndtime())){
             return  new ResultBody(CommonEnum.GAME_ACT_HDYJS);
         }
          // 返回 已开始活动
         return new ResultBody(CommonEnum.SUCCESS,cardGame);
  }
/**
 * 功能描述: 更据id 查询 活动的信息；
 * @Param: [cardGameId]
 * @Return: com.sykj.util.reponse.ResultBody
 * @Author: Feiwo Sicun
 * @Date: 2020/8/8 14:44
 */
  @GetMapping("/queryByid/{cardGameId}")
  public ResultBody queryByid(@PathVariable("cardGameId")String cardGameId){
      CardGame byId = cardGameService.getById(cardGameId);
      return  new ResultBody(CommonEnum.SUCCESS,byId);
  }
}
